package com.ask.forMe.config;

import com.ask.forMe.model.enums.Events;
import com.ask.forMe.model.enums.States;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.config.common.annotation.AnnotationBuilder;

@Configuration
@EnableStateMachine(name = "orderStateMachine")
public class StateMachineConfig extends StateMachineConfigurerAdapter<States, Events> {

    @Override
    public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
        states
                .withStates()
                .initial(States.UNPAID)
                .state(States.WAITING_FOR_DELIVERY)
                .state(States.READY_FOR_DELIVERY)
                .end(States.DELIVERED);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
        transitions
                .withExternal()
                .source(States.UNPAID).target(States.WAITING_FOR_DELIVERY)
                .event(Events.PAY)
                .and()
                .withExternal()
                .source(States.WAITING_FOR_DELIVERY).target(States.READY_FOR_DELIVERY)
                .event(Events.DELIVERY)
                .and()
                .withExternal()
                .source(States.READY_FOR_DELIVERY).target(States.DELIVERED)
                .event(Events.RECEIVE);
    }


}

